<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link rel="stylesheet" href="/css/elementUI.css">
    <script src="/js/vue.js"></script>
    <script src="/js/elementUI.js"></script>
    <script src="/js/axios.js"></script>
    <script src="/js/util.js"></script>
</head>
<body>

<div id="app">

    <el-input v-model="query.rname" style="width: 200px;" clearable placeholder="关键字查询"></el-input>


    <el-table
            :data="tableData"
            style="width: 100%">
        <el-table-column
                prop="id"
                label="角色ID"
                width="180">
        </el-table-column>
        <el-table-column
                prop="rname"
                label="角色名称">
        </el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                <el-button
                        size="mini"
                        type="warn" @click="editMenu(scope.row.id)">分配菜单</el-button>
            </template>
        </el-table-column>
    </el-table>



    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="query.page"
            :page-sizes="[1, 2, 3, 4]"
            :page-size="query.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>


    <el-dialog title="修改角色" :visible.sync="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="角色名称" :label-width="formLabelWidth">
                <el-input v-model="form.rname" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="updateUser">确 定</el-button>
        </div>
    </el-dialog>



    <el-dialog title="分配菜单" :visible.sync="roledialogFormVisible">


        <el-table
                :data="menuList" @selection-change="handleSelectionChange" ref="multipleTable"
                style="width: 100%">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="id"
                    label="菜单id"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="mname"
                    label="菜单名称">
            </el-table-column>
            <el-table-column
                    prop="url"
                    label="菜单URL">
            </el-table-column>

        </el-table>


        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="updateRoleMenu">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script>
    new Vue({
        el:"#app",
        data:{

            tableData:[],
            total:0,
            query:{
                rname:"",
                page:1,
                pageSize:2
            },


            dialogFormVisible: false,
            form: {
                rname: '',
            },
            formLabelWidth: '120px',


            roledialogFormVisible:false,
            menuList:[],
            multipleSelection:[],
            currentRid:"",//代表当前要修改的用户id

        },
        watch:{
            query:{
                deep:true,
                handler(){
                    this.initData()
                }
            }
        },
        methods:{
            initMenuList(){
                axios.post(`/menu/findAll`).then(res=>{
                    this.menuList = res.data;
                })
            },
            updateRoleMenu(){
                if(this.multipleSelection.length==0){
                    this.$message.info("请选种菜单")
                }else{
                    let uid = this.currentRid;
                    let rids = this.multipleSelection.map(m=>m.id).join(",");

                    axios.post(`/role/updateRoleMenu?rid=${uid}&mids=${rids}`).then(res=>{
                        this.roledialogFormVisible = false;
                    })
                }

            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            editMenu(id){
                this.currentRid = id;
                this.roledialogFormVisible = true;
                /**
                 * 1、展示弹框，为了表格组件注册成功
                 * 2、ajax查出现有角色
                 * 3、显示角色列表
                 * 4、回显现有角色
                 */

                axios.post("/role/findRoleMenu?id="+id).then(res=>{
                    let roleMenuList = res.data;
                    console.log(roleMenuList)

                    this.$refs.multipleTable.clearSelection();//清理选中的数据
                    let arr  = [];
                    for (let i = 0; i < roleMenuList.length; i++) {
                        let ur = roleMenuList[i];
                        // ur.rid//角色id  查找角色对象
                        for (let j = 0; j < this.menuList.length; j++) {
                            let r = this.menuList[j];//菜单
                            if(r.id==ur.mid){
                                arr.push(r);
                            }
                        }
                    }
                    arr.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });



                })


            },
            updateUser(){
                axios.post("/role/update",this.form).then(res=>{
                    this.$message({
                        type: 'success',
                        message: '修改成功!'
                    });
                    this.dialogFormVisible = false;
                    this.initData();
                })
            },
            handleEdit(index, row) {
                this.dialogFormVisible = true;
                this.form = JSON.parse(JSON.stringify(row));
            },
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post("/role/deleteOne?id="+row.id).then(res=>{
                        this.initData();
                        this.$message({
                            type: 'info',
                            message: res.data
                        });
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSizeChange(val) {
                this.query.pageSize = val
            },
            handleCurrentChange(val) {
                this.query.page = val
            },
            initData(){
                axios.post(`/role/findPage?page=${this.query.page}&pageSize=${this.query.pageSize}`,this.query).then(res=>{
                    console.log(res.data)
                    this.tableData = res.data.records;
                    this.total = res.data.total;

                    if(this.tableData.length==0&&this.query.page>1){
                        this.query.page--
                    }
                })

            }

        },
        created(){
            this.initData()
            this.initMenuList();
        }
    })
</script>

</body>
</html>